package com.BookManager.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration {
    @Bean
    public BCryptPasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Bean
    public PersistentTokenRepository tokenRepository(DataSource dataSource){
        JdbcTokenRepositoryImpl repository = new JdbcTokenRepositoryImpl();
        repository.setDataSource(dataSource);
//        repository.setCreateTableOnStartup(true);
        return repository;
    }

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity security,PersistentTokenRepository repository) throws Exception{
        return security
                .authorizeHttpRequests(auth -> {
                    auth.requestMatchers("/static/**").permitAll();
                    auth.anyRequest().authenticated();
                })
                .formLogin(conf -> {
                    conf.loginPage("/login")
                            .loginProcessingUrl("/doLogin")
                            .defaultSuccessUrl("/")
                            .permitAll();
                })
                .logout(conf -> {
                    conf.logoutUrl("/doLogout")
                            .logoutSuccessUrl("/login")
                            .permitAll();
                })
                .csrf(AbstractHttpConfigurer::disable)
                .rememberMe(conf -> {
                    conf.tokenRepository(repository)
                            .tokenValiditySeconds(3600 * 24 * 7);
                })
                .build();
    }

}
